package com.jeremyfeinstein.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jeremyfeinstein.gson.TBean;
import com.jeremyfeinstein.gson.User;
import com.orhanobut.logger.Logger;

import org.java_websocket.util.Base64;

import java.io.IOException;

public class ConfigUtil {

    private static final String TAG = ConfigUtil.class.getSimpleName();

    private static final String REMOTEIP = "REMOTEIP";
    private static final String REMOTEPORT = "REMOTEPORT";
    private static final String MCUPORT = "MCUPORT";
    private static final String TURNPORT = "TURNPORT";
    public static final String TCP = "TCP";
    public static final String MCU = "MCU";
    private static final String PORT1 = "SERVER_PORT1";
    private static final String PORT2 = "SERVER_PORT2";
    private static final String PORT3 = "SERVER_PORT3";
    private static final String PORT4 = "SERVER_PORT4";
    private static final String LOCAL_PORT3 = "LOCAL_PORT3";
    private static final String LOCAL_PORT4 = "LOCAL_PORT4";
    private static final String LOCAL_PORT5 = "LOCAL_PORT5";
    private static final String AGENT_SERVER = "AGENT_SERVER";
    private static final String LOGINUSER = "LOGINUSER";
    public static final String AUTOLOGIN = "AUTOLOGIN";
    private static final String SESSIONID = "sessionid";
    private static final String MOBILEID = "mobileid";
    private static final String RESOLUTION = "resolution";
    public static final String REMEMBER_LOGIN_CODE = "remember_login_code";
    private static final String IP_CHANGE_FALG = "ipChangeFlag";
    public static final String GETMSG = "GETMSG";
    private static final String MCUSERVERIP = "mcuserverip";
    private static final String COMPRESSTYPE = "compresstype";
    private static final String LOCALSOCKETURL = "localsocketurl";
    private static final String REMOTESOCKETURL = "remotesocketurl";

    private static ConfigUtil instance;
    private SharedPreferences pref;
    private Gson mGson;
    private String meeting_resource;
    private static final String APP_SERVER_HTTP_PORT = "app_server_http_port"; //二级互动的http端口
    private static final String APP_SERVER_HTTPS_PORT = "app_server_http_port"; //二级互动的https端口

    private ConfigUtil() {
        pref = ConferenceApp.getApp().getSharedPreferences("config", Context.MODE_PRIVATE);
        mGson = new GsonBuilder().create();
    }

    public static ConfigUtil getInstance() {
        if (instance == null) {
            instance = new ConfigUtil();
        }
        return instance;
    }

    public void setAppServerHttpPort(String port) {
        Editor edit = pref.edit();
        edit.putString(APP_SERVER_HTTP_PORT, port);
        edit.apply();
    }

    public String getAppServerHttpPort() {
        return pref.getString(APP_SERVER_HTTP_PORT, "");
    }

    public void setAppServerHttpsPort(String port) {
        Editor edit = pref.edit();
        edit.putString(APP_SERVER_HTTPS_PORT, port);
        edit.apply();
    }

    public String getAppServerHttpsPort() {
        return pref.getString(APP_SERVER_HTTPS_PORT, "");
    }

    public void setMsg(boolean msg) {
        Editor edit = this.pref.edit();
        edit.putBoolean("GETMSG", msg);
        edit.apply();
    }

    public boolean getMsg() {
        return this.pref.getBoolean("GETMSG", true);
    }

    public void setIpChangeFlag(boolean ipChangeFlag) {
        Editor edit = pref.edit();
        edit.putBoolean(IP_CHANGE_FALG, ipChangeFlag);
        edit.apply();
    }

    public void setLoginUser(User loginUser) {
        if (loginUser == null) return;
        Editor edit = pref.edit();
        edit.putString(LOGINUSER, mGson.toJson(loginUser));
        edit.apply();
    }

    public void setResolution(String resolution) {
        Editor edit = pref.edit();
        edit.putString(RESOLUTION, resolution);
        edit.apply();
    }

    public String getResolution() {
        return pref.getString(RESOLUTION, "标清(640)");
    }

    public User getLoginUser() {
        User loginUser = null;
        String jsonUser = pref.getString(LOGINUSER, null);
        if (!TextUtils.isEmpty(jsonUser)) {
            loginUser = mGson.fromJson(jsonUser, User.class);
        }
        return loginUser;
    }

    public void setSessionId(String sessionid) {
        Editor edit = pref.edit();
        edit.putString(SESSIONID, sessionid);
        edit.apply();
    }

    public String getSessionId() {
        return pref.getString(SESSIONID, "");
    }

    public String getRemoteUrl() {
        return SPUtil.get(REMOTEIP, "").toString();
    }

    public String getRemotePort() {
        return SPUtil.get(REMOTEPORT, "").toString();
    }

    public void setAgentServer(String server) {

        Editor edit = pref.edit();
        edit.putString(AGENT_SERVER, server);
        edit.apply();
    }

    public String getAgentServer() {
       return pref.getString(AGENT_SERVER, "");
    }

    public void setLocalPort(String port1, String port2) {

        Editor edit = pref.edit();
        edit.putString(LOCAL_PORT3, port1);
        edit.putString(LOCAL_PORT4, port2);
        edit.apply();
    }


    public void setServerPort(String port1, String port2, String port3,
                              String port4) {

        Editor edit = pref.edit();
        edit.putString(PORT1, port1);
        edit.putString(PORT2, port2);
        edit.putString(PORT3, port3);
        edit.putString(PORT4, port4);
        edit.apply();
    }

    public String getLocalPort3() {
       return pref.getString(LOCAL_PORT3, "");
    }

    public String getLocalPort4() {
        return pref.getString(LOCAL_PORT4, "");
    }


    public String getMCUPort1() {
        return pref.getString(PORT1, "");
    }

    public String getMCUPort2() {
       return pref.getString(PORT2, "");
    }

    public String getMCUPort3() {
        return pref.getString(PORT3, "");
    }


    public String getTurnPort() {
        return pref.getString(TURNPORT, "3478");
    }


    public void setRemoteUrl(String remoteurl) {
        SPUtil.put(REMOTEIP, remoteurl);
    }

    public void setRemotePort(String remoteport) {
        SPUtil.put(REMOTEPORT, remoteport);
    }

    public void setMCUPort(String remoteport) {
        Editor edit = pref.edit();
        edit.putString(MCUPORT, remoteport);
        edit.apply();
    }


    public void setTurnPort(String turnPort) {
        Editor edit = pref.edit();
        edit.putString(TURNPORT, turnPort);
        edit.apply();
    }


    public static String setMCUIP(String t, String mcu_ip, String mcu_port) {
        try {
            t = new String(Base64.decode(t));
            Logger.e("decode tooken:" + t);
            TBean tBean = ConfigUtil.getInstance().mGson.fromJson(t, TBean.class);
            String[] h;
            if (!TextUtils.isEmpty(mcu_ip)) {
                h = tBean.getHost().split(":");
                tBean.setHost(mcu_ip + ":" + h[1]);
            }
            if (!TextUtils.isEmpty(mcu_port)) {
                h = tBean.getHost().split(":");
                tBean.setHost(h[0] + ":" + mcu_port);
            }

            t = ConfigUtil.getInstance().mGson.toJson(tBean);
            Logger.e("setMCUIP tooken:" + t);
            t = Base64.encodeBytes(t.getBytes());
            Logger.e("encode tooken:" + t);
        } catch (IOException e1) {
            Log.e(TAG, "setMCUIP:IOException " + e1.getMessage());
        }
        return t;
    }

    public static String getRoleName(String roleCode) {
        String roleName = "听众";
        if ("1".equals(roleCode)) {
            roleName = "管理员";
        } else if ("2".equals(roleCode)) {
            roleName = "助理";
        } else if ("3".equals(roleCode)) {
            roleName = "参会人员";
        }
        return roleName;
    }


    public void setMobileId(String mobileId) {
        Editor edit = pref.edit();
        edit.putString(MOBILEID, mobileId);
        edit.apply();
    }

    public String getMobileId() {
        return pref.getString(MOBILEID, "");
    }

    public String getMeeting_resource() {
        return meeting_resource;
    }

    public void setMeeting_resource(String meeting_resource) {
        this.meeting_resource = meeting_resource;
    }

    public void setMcuServerIP(String mcuServer) {
//        String mcuServerIP = "";
//        try {
//            mcuServerIP = threeDESEncryptionUtil.encryptThreeDESECB(URLEncoder.encode(mcuServer, "UTF-8"), ALGORITHM);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        Editor edit = pref.edit();
        edit.putString(MCUSERVERIP, mcuServer);
        edit.apply();

    }

    public String getMcuServerIP() {
        return pref.getString(MCUSERVERIP, "");
//        String mcuServerIP = "";
//        try {
//            byte[] bytes = threeDESEncryptionUtil.decryptThreeDESECB(mcuServerIPString, ALGORITHM);
//            mcuServerIP = new String(bytes);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return mcuServerIP;
    }


    public void setCompressType(String type) {
        Editor edit = pref.edit();
        if (!TextUtils.isEmpty(type)) {
            edit.putString(COMPRESSTYPE, type);
        }
        edit.apply();
    }

    public String getCompressType() {
        return pref.getString(COMPRESSTYPE, "0");
    }

    public void saveLocalSocketUrl(String url) {
        Editor edit = pref.edit();
        if (!TextUtils.isEmpty(url)) {
            edit.putString(LOCALSOCKETURL, url);
        }
        edit.apply();
    }

    public String getLocalSocketUrl() {
        return pref.getString(LOCALSOCKETURL, "");
    }

    public void saveRemoteSocketUrl(String url) {
        Editor edit = pref.edit();
        if (!TextUtils.isEmpty(url)) {
            edit.putString(REMOTESOCKETURL, url);
        }
        edit.apply();
    }

    public String getRemoteSocketUrl() {
        return pref.getString(REMOTESOCKETURL, "");
    }


}
